﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using System.Collections;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;



namespace Tosi
{
    class WriteToExcel
    {
        //private Document doc;
        private Excel.Application excApp;
        private Excel._Workbook excWb;
        private Excel._Worksheet excWs;
        private int writeHere = 2;

        public WriteToExcel(){
            //doc = new Document();
            //PdfWriter.GetInstance(doc, new FileStream("../../TOSI.pdf", FileMode.Create));
            //doc.SetPageSize(iTextSharp.text.PageSize.A2.Rotate());
            //doc.Open();
            excApp = new Excel.Application();
            excWb = (Excel._Workbook)(excApp.Workbooks.Add(Missing.Value));
            excWs = (Excel._Worksheet)excWb.ActiveSheet;
            excApp.Visible = true;  
        }

        /*public void fill(double [,] array, int nrOfColumns, int nrOfRows,int ind){
            Paragraph t = new Paragraph("Individum"+ (ind+1) + "\n\n");
            t.Font.SetStyle(Font.ITALIC);
            doc.Add(t);
            PdfPTable table = new PdfPTable(nrOfColumns + 1);
            table.WidthPercentage = 100;
            PdfPCell cell = new PdfPCell();
            cell.HorizontalAlignment = 1;
            table.HorizontalAlignment = 0;
            table.AddCell(cell);
            cell.Border = 0;
            cell.Colspan = nrOfColumns;
            for (int i = 0; i < nrOfColumns; i++)
                table.AddCell("Light " + i);
                for (int i = 0; i < nrOfRows; i++)
                {
                    for (int j = 0; j < nrOfColumns; j++)
                    {
                        if (j == 0)
                            table.AddCell("Weight " + i);   
                            table.AddCell(array[j,i].ToString("F2"));
                    }
                }
            doc.Add(table);
            doc.Add(new Paragraph("\n\n"));
            //Paragraph subtitle = new Paragraph("Magyarazat\n");
            //subtitle.Font.SetStyle(Font.BOLD);
            //doc.Add(subtitle);
            //doc.Add(new Paragraph("\r Lorem ipsum dolor sit amet, tale concludaturque qui ex. Habeo appetere consequuntur nec no, ei quo equidem voluptaria, nec ex epicurei tincidunt. Cu per mutat fabulas consequuntur, essent maiestatis an nam. Ut regione facilisi salutandi quo. Est ei diceret invidunt contentiones, pro et appareat voluptatum.Wisi ipsum at vix, te tale placerat complectitur sit, duo in idque mazim. Te vix option volumus deterruisset, nostrud albucius repudiare ea eam, ut veri erroribus usu. Dictas abhorreant ex qui, in ludus legere aliquid cum. Sit nusquam deseruisse et. Cu mea mediocrem deseruisse. Ut sanctus consulatu sententiae mel, ne qui wisi iriure latine."));
        }*/
        public void writeGeneration(int gen)
        {
            excWs.Cells[1, 1] = gen;
            excWs.Cells[writeHere++, 1] = "Generation " + gen;
        }
        public void writeHeader1(int id)
        {
            excWs.Cells[++writeHere, 1] = "Individual " + id;
            writeHere++;
            for (int i = 0; i < Var.StrNumber; i++)
            {
                excWs.Cells[writeHere, i + 3] = "Lampa " + (i + 1);
            }
            writeHere++;
            excWs.Cells[writeHere, 2] = "RedRule 1";
            excWs.Cells[writeHere + 1, 2] = "RedRule 2";
            excWs.Cells[writeHere + 2, 2] = "RedRule 3";
            excWs.Cells[writeHere + 3, 2] = "GreenRule 1";
            excWs.Cells[writeHere + 4, 2] = "GreenRule 2";
            excWs.Cells[writeHere + 5, 2] = "GreenRule 3";
        }
        public void writeWeight(int id,double[] weight)
        {
            excWs.Cells[writeHere, 3 + id] = weight[0];
            excWs.Cells[writeHere + 1, 3 + id] = weight[1];
            excWs.Cells[writeHere + 2, 3 + id] = weight[2];
            excWs.Cells[writeHere + 3, 3 + id] = weight[3];
            excWs.Cells[writeHere + 4, 3 + id] = weight[4];
            excWs.Cells[writeHere + 5, 3 + id] = weight[5];
        }
        public void lineFeed1()
        {
            writeHere += 7;
        }
        
        public void writeHeader2()
        {
            writeHere++;
            excWs.Cells[writeHere, 2] = "WaitCars";
            excWs.Cells[writeHere + 1, 2] = "WaitTime";
            excWs.Cells[writeHere + 2, 2] = "StreetLoad";
        }
        public void writeHeader3()
        {
            writeHere++;
            for (int i = 0; i < Var.JncNumber; i++)
            {
                excWs.Cells[writeHere, i + 3] = "Csp " + (i + 1);
            }
            writeHere++;
            excWs.Cells[writeHere, 2] = "AvgWaitCars";
            excWs.Cells[writeHere + 1, 2] = "AvgWaitTime";
            excWs.Cells[writeHere + 2, 2] = "JuncConges";
            excWs.Cells[writeHere + 3, 2] = "Global";
        }
        public void writeLightTosi(int id, double[] t)
        {
            excWs.Cells[writeHere, 3 + id] = t[0];
            excWs.Cells[writeHere + 1, 3 + id] = t[1];
            excWs.Cells[writeHere + 2, 3 + id] = t[2];
        }
        public void lineFeed3()
        {
            writeHere += 3;
        }
        public void writeJuncTosi(int id, double[] t)
        {
            excWs.Cells[writeHere, 3 + id] = t[0];
            excWs.Cells[writeHere + 1, 3 + id] = t[1];
            excWs.Cells[writeHere + 2, 3 + id] = t[2];
            excWs.Cells[writeHere + 3, 3 + id] = t[3];
        }
        public void lineFeed2()
        {
            writeHere += 5;
        }
        public void footer(double tosi)
        {
            excWs.Cells[writeHere, 1] = "TOSI";
            excWs.Cells[writeHere, 2] = tosi;
            writeHere += 2;

        }
        public void closeFile() {
            //doc.Close();
        }
        public void writeIndividual(int individ) {
            excWs.Cells[1, 2] = individ;
        }
        public void writeIteration(int it) {
            excWs.Cells[1, 3] = it;
        }
        public void writeJuncTosi(int id, double t1, double t2, double t3,double t4) {
            excWs.Cells[writeHere, id] = t1;
            excWs.Cells[writeHere+1, id] = t2;
            excWs.Cells[writeHere+2, id] = t3;
            excWs.Cells[writeHere+3, id] = t4;
        }
    }
}
